package com.shsun.curveservice.logic.async.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shsun.curveapi.domain.bo.TaskBO;
import com.shsun.curveservice.common.enum1.TimeUnitEnum;
import com.shsun.curveservice.domain.entity.CurveDetail;
import com.shsun.curveservice.domain.entity.TaskCalendar;
import com.shsun.curveservice.logic.async.TaskCalendarAsyncService;
import com.shsun.curveservice.logic.service.CurveDetailService;
import com.shsun.curveservice.logic.service.TaskCalendarService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 异步生成日历服务
 *
 * @author shsun
 * @date 2024/06/12
 */
@Slf4j
@Service
public class TaskCalendarAsyncServiceImpl implements TaskCalendarAsyncService {
    @Resource
    private CurveDetailService curveDetailService;
    @Resource
    private TaskCalendarService taskCalendarService;
    /**
     * 异步生成日历
     *
     * @date 2024/06/12
     * @author shsun
     */
    @Async
    @Override
    public void generateTaskCalendar(TaskBO addTaskDTO) {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = localDateTime.toLocalDate();
        Long ruleId = addTaskDTO.getRuleId();
        if (ruleId == null) {
            // 说明是一个任务，没有反复执行的周期，就是指定日期的一个任务
            TaskCalendar taskCalendar = new TaskCalendar();
            taskCalendar.setDate(addTaskDTO.getEndDate());
            taskCalendar.setTaskId(addTaskDTO.getTaskId());
            taskCalendar.setRuleId(ruleId);
            taskCalendarService.save(taskCalendar);
            return;
        }
        // 创建LambdaQueryWrapper实例
        LambdaQueryWrapper<CurveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CurveDetail::getRuleId,ruleId);
        // 添加排序条件，例如根据字段 "field_name" 进行降序排序
        queryWrapper.orderByDesc(CurveDetail::getRuleId);
        // 使用Service的list方法，传入条件构造器
        List<CurveDetail> list = curveDetailService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (CurveDetail curveDetail : list) {
            Byte value = curveDetail.getValue();
            // com.shsun.curveservice.common.enum1.TimeUnitEnum中的值
            Byte unit = curveDetail.getUnit();
            TimeUnitEnum timeUnitEnum = TimeUnitEnum.fromCode(unit);
            LocalDateTime dateTime =switch (timeUnitEnum) {
                case MINUTES->localDateTime.plusMinutes(value);
                case HOURS->localDateTime.plusHours(value);
                case DAYS->localDateTime.plusDays(value);
                default->null;
            };

            TaskCalendar taskCalendar = new TaskCalendar();
            taskCalendar.setDate(dateTime);
            taskCalendar.setTaskId(addTaskDTO.getTaskId());
            taskCalendar.setRuleId(ruleId);
            taskCalendarService.save(taskCalendar);
        }
    }
}
